<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class MY_Controller extends Controller{

	var $data = array();
	
    function MY_Controller(){
        parent::Controller();
    }
    
    function validate_cookie(){
    	$this->load->library('session');
    	
    	//check login, no logged redirect to homepage
    	if (!$this->session->userdata('logged') || !$this->session->userdata('usrname')){
    		if (current_url() != base_url()) redirect(base_url());
    	}
    }

	/**
	 * router signature call function from ajax request
	 * @param unknown_type $func_name
	 */
	function ajax($func_name, $a=false, $b=false, $c=false, $d=false){
		$this->{'_'.$func_name}($a, $b, $c, $d);
	}
	
	/**
	 * print semua view.
	 * tidak digunakan untuk ajax
	 * @param $view
	 */
	function finalize_view(){
		if (!$this->data['page']['content']){
			$this->data['page']['content'] = $this->load->view($this->data['page']['template_view'], $this->data['layout'], true);
		}
		$this->finalize_view_str(
			$this->load->view('main_page', $this->data['page'], true) );
	}
	
	function finalize_view_str($str){
		//FIXME hilangkan html coment <!-- -->
		//$str = preg_replace('/\<\!\-\-(.*)\-\-\>/i','',$str);
		//$str = str_replace(array("\r\n","\r","\s+","\t"),"",$str);
		echo $str;
	}
	
	function initialize_page($data){
		$this->load->config('rnd');
		$config_def = $this->config->item('rnd');
		$config_def = $config_def['page'];
		
		//set default value
		$this->data['page'] = $config_def;
		
		if (isset($data['jscript'])){
			$this->data['page']['jscript'] = js_script($data['jscript']);
		}
		if (isset($data['css'])){
			$this->data['page']['css'] = css_script($data['css']);
		}
		foreach(array('template_view','content','title') as $key){
			$this->data['page'][$key] = isset($data[$key])? $data[$key] : $config_def[$key];
		}
	}
	
	function initialize_layout($data){
		$this->load->config('rnd');
		$config_def = $this->config->item('rnd');
		$config_def = $config_def['layout'];
		
		//set default value
		$this->data['layout'] = $config_def;
		 
		foreach(array('left_width','right_width') as $key){
			$this->data['layout'][$key] = isset($data[$key])? $data[$key] : $config_def[$key];
		}
		
		if (isset($data['left_content'])){
			$this->data['layout']['left_content'] = $this->load->view(
				$data['left_content']['view'], 
				$data['left_content']['view_data'], true);
		}
		
		if (isset($data['right_content'])){
			$this->data['layout']['right_content'] = $this->load->view(
				$data['right_content']['view'], 
				$data['right_content']['view_data'], true);	
		}
	}
	
	/**
	 * show fb confirm dialog
	 * @param $title - title of confirm modal
	 * @param $message - message of the confirm dialog
	 * @param $action - form url action
	 * @param $callback - javascript callback function when yes button click
	 * @param $formId - the property id of the element form
	 */
	function _confirmDialog(){
		$title = $this->input->post('title');
		$message = $this->input->post('message');
		$callback = $this->input->post('callback');
		
		$this->load->view('utils/fbModal_confirm', 
			array('title'=>$title, 
				'message'=>$message, 
				'callback'=>$callback)
			);
	}
	
	function _messageDialog(){
		$title = $this->input->post('title');
		$message = $this->input->post('message');
		$this->load->view('utils/fbModal_message', array('title'=>$title, 'message'=>$message));
	}
}
/* End of file MY_Controller.php */
/* Location: ./system/application/libraries/MY_Controller.php */